// pages/movies/movies.js
var util = require('../../utils/util.js')
var app = getApp();

Page({


  // 所需数据结构
  // data : {
  //  inTheaters: {},
  //  commingSoon: {},
  //  top250: {}
  // }

  /**
   * 页面的初始数据
   */
  data: {
    containerShow: true,
    // 是否显示搜索结果
    searchPanelShow: false,
    searchResult: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 正在热映
    var inTheatersUrl = app.globalData.doubanBase +
      "/v2/movie/in_theaters" + "?start=0&count=3";
    console.log("inTheatersUrl = " + inTheatersUrl);
    // 即将上映
    var comingSoonUrl = app.globalData.doubanBase +
      "/v2/movie/coming_soon" + "?start=0&count=3";
    // top250
    var top250Url = app.globalData.doubanBase +
      "/v2/movie/top250" + "?start=0&count=3";

    // 请求数据
    this.getMovieListData(inTheatersUrl, "inTheaters", "正在热映");
    this.getMovieListData(comingSoonUrl, "comingSoon", "即将上映");
    this.getMovieListData(top250Url, "top250", "豆瓣Top250");
  },

  getMovieListData: function(url, setterKey, categoryTitle) {
    var that = this;
    wx.request({
      url: url,
      method: "GET",
      header: {
        "content-type": "json"
      },
      success: function(res) {
        // console.log("请求成功: " + res.data);
        that.processDoubanData(res.data, setterKey, categoryTitle)
      },
      fail: function(error) {
        console.log("请求失败: " + error);
      }
    })
  },

  processDoubanData: function(moviesDouban, settedKey, categoryTitle) {

    var movies = [];
    // 遍历, 处理数据
    for (var idx in moviesDouban.subjects) {
      var subject = moviesDouban.subjects[idx];
      var title = subject.title;
      // 处理标题
      if (title.length >= 6) {
        title = title.substring(0, 6) + "...";
      }
      var temp = {
        stars: util.convertToStarsArray(subject.rating.stars),
        title: title,
        average: subject.rating.average,
        coverageUrl: subject.images.large,
        movieId: subject.id
      }

      movies.push(temp);
      var readyData = {};
      readyData[settedKey] = {
        categoryTitle: categoryTitle,
        movies: movies
      }
      this.setData(readyData);
    }
  },

  onMoreTap: function(options) {
    var category = options.currentTarget.dataset.category;
    wx.navigateTo({
      url: 'more-movies/more-movies?category=' + category,
    })
  },

  onCancelImgTap: function(event) {
    console.log("onCancelImgTap");

    this.setData({
      containerShow: true,
      searchPanelShow: false,
      searchResult: {}
    })
  },

  // 成为焦点
  onBindFocus: function(event) {
    console.log("onBindFocus");
    this.setData({
      containerShow: false,
      searchPanelShow: true
    })
  },

  // 失去焦点
  onBindBlur: function(event) {
    console.log("onBindBlur");
    var text = event.detail.value;
    var searchUrl = app.globalData.doubanBase + "/v2/movie/search?q=" + text;
    // 请求数据
    this.getMovieListData(searchUrl, "searchResult", "");
  },

  // 单个条目点击
  onMovieTap: function(event) {
    var movieId = event.currentTarget.dataset.movieid;
    wx.navigateTo({
      url: "movie-detail/movie-detail?id=" + movieId
    })
  }
})